import request from '@/utils/request'
// import type Menu from 'element-plus/es/components/menu/src/utils/menu-bar.mjs'
type Common<T> = {
    code: string
    data: T
    mesg: string
    time: string
}
export type MenuItem = {
    createdBy: string
    createdTime: string
    description: string
    href: string
    icon: string
    id: number
    level: number                //菜单级别
    name: string
    operatorId: number | null   //联合类型可能是数值也可能是null
    orderNum: number            //顺序
    parentId: number            //父一级层级
    shown: boolean
    updatedBy: string
    updatedTime: string
}
// 定义创建或编辑菜单的所需类型 1.partial部分可选 2.pick挑选 3.Omit
// export type CreateOrEditMenu = Partial<MenuItem>
export type CreateOrEditMenu = Pick<MenuItem,"name"|"href"|"parentId"|"description"|"icon"|"shown"|"orderNum"> &{ id?: number }
// export type CreateOrEditMenu = Omit<MenuItem,"createdBy"|"createdTime"|"level"|"updatedTime">
// 获取所有菜单逻辑
export const getAll = () => {
    return request<Common<MenuItem[]>>({//类型约束
        method: 'GET',
        url: '/boss/menu/getAll'
    })
}
// 创建或编辑菜单的逻辑
export const saveOrUpdate = (menuInfo:CreateOrEditMenu) => {
    return request<Common<boolean>>({
        method:'POST',
        url:'/boss/menu/saveOrUpdate',
        data:menuInfo
    })
}
// 接口函数删除菜单逻辑
export const deleteMenu = (id: MenuItem["id"]) => {
    return request<Common<boolean>>({
        method: 'DELETE',
        url: `/boss/menu/${id}`
        // 加反引号看是否是动态变量
    })
}

//定义类型
type subMenuItem = MenuItem & {subMenuList:subMenuItem [] | null}
type EditMenuInfo = Common<{menuInfo:MenuItem,parentMenuList:subMenuItem[]}>
//获取指定ID的编辑菜单
export const getEditMenuInfo = ( id:string ) => {
    return request ({
        method:'GET',
        url:'/boss/menu/getEditMenuInfo',
        params:{ id } // 以？的形式携带数据
    })
}